﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Devfw.OpenAuth;
using System.Web;
using System.Net;

namespace Devfw.OpenAuth.Platform
{
    public  class KaiXin001V2 : ProviderBase
    {
        public const string UserInfoFields = "uid,name,gender,hometown,city,status,logo120,logo50";

        public KaiXin001V2( string oauth_consumer_key, string oauth_consumer_secret )
            : base( oauth_consumer_key, oauth_consumer_secret )
        {
        }

        public KaiXin001V2(string oauth_consumer_key, string oauth_consumer_secret, string oauth_token, string oauth_token_secret)
            : base( oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret )
        {
        }


        #region OAuth
        public RequestTokenResponse request_token(string authCallBack)
        {
            return _core.request_token("http://api.kaixin001.com/oauth/request_token", authCallBack, new ParameterCollection()
                {
                    new Parameter("scope", "basic create_records", ParameterType.Get)
                });
        }

        public void authorize( string oauth_token, string oauth_callback )
        {
            string url = _core.generate_authorize_url("http://api.kaixin001.com/oauth/authorize", oauth_token, oauth_callback);
            HttpContext.Current.Response.Redirect(url);
        }

        public string GetAuthorizeUrl(string  requestToken, string oauth_callback)
        {
            if (!string.IsNullOrEmpty(requestToken))
            {
                return _core.generate_authorize_url("http://api.kaixin001.com/oauth/authorize", requestToken, oauth_callback);
            }

            return string.Empty;
        }

        public AccessTokenResponse access_token( string oauth_token, string oauth_token_screct, string oauth_verifier )
        {
            return _core.access_token("http://api.kaixin001.com/oauth/access_token", oauth_token, oauth_token_screct, oauth_verifier);
        }
        #endregion

        #region 微博

        #region 发布微博
        public string records_add(string format, string content)
        {
            _core.AuthorizationType = AuthorizationType.UrlQuery;
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://api.kaixin001.com/records/add." + format, OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter( "content", content, ParameterType.Post )
                } );

            return response;
        }

        public string records_add(string format, string content, byte[] imageByte, string fileName)
        {
            var contentEncoding = "iso-8859-1";
            string fileData = System.Text.Encoding.GetEncoding( contentEncoding ).GetString( imageByte );

            _core.AuthorizationType = AuthorizationType.UrlQuery;
            string response = _core.OpenAuthFormDataPostWebRequest("http://api.kaixin001.com/records/add." + format, OAuthToken, OAuthTokenSecret,
            new ParameterCollection()
                {
                    new PostParameter( "content", content  ),
                    new PostParameter( "pic", fileData, true,  "filename=\"" + fileName + "\"", false )
                } );

            return response;
        }

        public string records_add(string format, string status, string url)
        {
            WebClient client = new WebClient();
            byte[] b = client.DownloadData( url );
            string[] f = url.Split( '\\', '/' );
            return records_add(format, status, b, f[f.Length - 1]);
        }
        #endregion

        #region 转发微博
        /// <summary>
        /// 转发微博
        /// </summary>
        /// <param name="id">微博id</param>
        /// <param name="status">添加的评论</param>
        /// <param name="is_comment"></param>
        /// <returns></returns>
        public string StatusesRepost(string format, string id, string status)
        {
            _core.AuthorizationType = AuthorizationType.UrlQuery;
            return string.Empty;

            //string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://api.t.sina.com.cn/statuses/repost." + format, OAuthToken, OAuthTokenSecret,
            //    new ParameterCollection()
            //    {
            //        new Parameter( "status", HttpUtility.UrlEncode( status ), ParameterType.Get ),
            //        new Parameter( "source", HttpUtility.UrlEncode( _core.OAuthConsumerKey), ParameterType.Get ),
            //        new Parameter( "id", id, ParameterType.Get ),
            //        new Parameter( "is_comment", is_comment, ParameterType.Get )
            //    });

            //return response;
        }
        #endregion

        #region 评论微博
        /// <summary>
        /// 评论微博
        /// </summary>
        /// <param name="format"></param>
        /// <param name="id">要评论的微博消息ID</param>
        /// <param name="comment">评论内容。必须做URLEncode,信息内容不超过140个汉字。</param>
        /// <returns></returns>
        public string StatusesComment(string format, string id, string comment)
        {
            _core.AuthorizationType = AuthorizationType.UrlQuery;
            return string.Empty;
            //string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://api.t.sina.com.cn/statuses/comment." + format, OAuthToken, OAuthTokenSecret,
            //    new ParameterCollection()
            //    {
            //        new Parameter( "comment", HttpUtility.UrlEncode( comment ), ParameterType.Get ),
            //        new Parameter( "source", HttpUtility.UrlEncode( _core.OAuthConsumerKey), ParameterType.Get ),
            //        new Parameter( "id", id, ParameterType.Get ),
            //        new Parameter( "without_mention", "0", ParameterType.Get ),
            //        new Parameter( "comment_ori", commentOri, ParameterType.Get )
            //    });

            //return response;
        }
        #endregion

        #endregion

        public string users_show(string format, string id)
        {
            _core.AuthorizationType = AuthorizationType.UrlQuery;
            string response = _core.OpenAuthWebRequest(HttpWebMethod.GET, string.Format("http://api.kaixin001.com/users/show.{0}", format), OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter( "uids", HttpUtility.UrlEncode( _core.OAuthConsumerKey), ParameterType.Get ),
                    new Parameter( "fields", UserInfoFields, ParameterType.Get )
                });

            return response;
        }


        public string users_me(string format)
        {
            _core.AuthorizationType = AuthorizationType.UrlQuery;
            string response = _core.OpenAuthWebRequest(HttpWebMethod.GET, string.Format("http://api.kaixin001.com/users/me.{0}", format), OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter( "fields", UserInfoFields , ParameterType.Get )
                });

            return response;
        }
    }
}
